home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / packet / terminal / pr4w32 / source / srvdll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-21  |  15.8 KB  |  380 lines

  1. /*****************************************************************************/
  2. /* PROJEKT PR4WIN  Service DLL                                               */
  3. /*                                                                           */
  4. /* This is a sample for a Pr4Win Service implemented as a DLL                */
  5. /*                                                                           */
  6. /*                                                                           */
  7. /*                                                                           */
  8. /*                                                                           */
  9. /*                                                                           */
  10. /* BY:                                                                       */
  11. /* ----------                                                                */
  12. /* OE8DJK Bernd M. Stroj         18.01.1996                                  */
  13. /*                                                                           */
  14. /*****************************************************************************/
  15.  
  16. #include "windows.h"
  17. #include "stdlib.h"
  18. #include "string.h"
  19. #include "srvdll.h"
  20.  
  21.  
  22.  
  23. #define  VERSION  102                             // becones 1.02 in remote conmmand //SERV
  24.  
  25. #define  CR       13
  26. #define  LF       10
  27.  
  28.  
  29. typedef struct
  30.    {
  31.      long         hCon;                           // Handle to Connection
  32.      char         szCallSign[10];                 // E.G."OE8DJK-15"
  33.      char         szInpBuffer[128];               // contains complete inputline
  34.      short        sCount;                         // Characters in szInpBuffer
  35.      void         *ptPrivat;                      // pointer to private data 
  36.      void         *ptNextCon;                     // Daisy chain
  37.    } CON_DATA;
  38.  
  39. static   CON_DATA     *ptFirstCon = NULL;
  40.  
  41.  
  42. static   FARPROC      lpfnSnd2Con;                // Service -> Connect
  43. static   FARPROC      lpfnControl;                // Service connection
  44.  
  45.  
  46. static void      vAddCon      ( CON_DATA *ptCon );
  47. static void      vDelCon      ( long hCon );
  48. static CON_DATA *ptGetCon     ( long hCon );
  49. static void      vService     ( long hCon, CON_DATA *ptCon );
  50. static void      vExitService ( long hCon, CON_DATA *ptCon );
  51.  
  52.  
  53.  
  54.  
  55. /*----------------------------------------------------------------------------*/
  56. /* Procedure:  LibMain                                                        */
  57. /*             Called by LibEntry, when DLLis loaded                          */
  58. /*                                                                            */
  59. /* Parameters: hModule          Module Handle                                 */
  60. /*             wDataSeg         Data Segment                                  */
  61. /*             cbHeapSize       Heap Size                                     */
  62. /*             lpszCmdLine      Command Line                                  */
  63. /*                                                                            */
  64. /* Returns:    1 wenn OK                                                      */
  65. /*                                                                            */
  66. /*----------------------------------------------------------------------------*/
  67.  
  68. int FAR PASCAL LibMain (HANDLE hModule, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine)
  69. {
  70.   return (1);                                      
  71. }
  72.  
  73. /*----------------------------------------------------------------------------*/
  74. /*                                                                            */
  75. /*   FUNCTION: DllMain(HANDLE, DWORD, LPVOID)                                 */
  76. /*                                                                            */
  77. /*   PURPOSE:  DllMain is called by Windows when                              */
  78. /*             the DLL is initialized, Thread Attached, and other times.      */
  79. /*             Refer to SDK documentation, as to the different ways this      */
  80. /*             may be called.                                                 */
  81. /*                                                                            */
  82. /*             The DllMain function should perform additional initialization  */
  83. /*             tasks required by the DLL.  In this example, no initialization */
  84. /*             tasks are required.  DllMain should return a value of 1 if     */
  85. /*             the initialization is successful.                              */
  86. /*                                                                            */
  87. /*                                                                            */
  88. /*----------------------------------------------------------------------------*/
  89.  
  90. BOOL APIENTRY DllMain(HANDLE hInst, DWORD ul_reason_being_called, LPVOID lpReserved)
  91. {
  92.     return 1;
  93.         UNREFERENCED_PARAMETER(hInst);
  94.         UNREFERENCED_PARAMETER(ul_reason_being_called);
  95.         UNREFERENCED_PARAMETER(lpReserved);
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. /*----------------------------------------------------------------------------*/
  103. /* Procedure:  iInitDll                                                       */
  104. /*             Initialisiert den Service, meldet Callback Fkt an.             */
  105. /*                                                                            */
  106. /* Parameters: ptInitServer                                                   */
  107. /*                                                                            */
  108. /* Returns:    1 if OK                                                        */
  109. /*                                                                            */
  110. /*----------------------------------------------------------------------------*/
  111.  
  112.  
  113. int APIENTRY iInitDll ( INIT_SERVICE *ptInitSrv )
  114. {
  115.    lpfnSnd2Con = ptInitSrv->lpfnAns;
  116.    lpfnControl = ptInitSrv->lpfnCtl;
  117.    return (1);
  118. }
  119.  
  120.  
  121. /*----------------------------------------------------------------------------*/
  122. /* Procedure:  iExitDll                                                       */
  123. /*                                                                            */
  124. /* Parameters: ptInitServer                                                   */
  125. /*                                                                            */
  126. /* Returns:    1 if OK                                                        */
  127. /*                                                                            */
  128. /*----------------------------------------------------------------------------*/
  129.  
  130.  
  131. int APIENTRY iExitDll ( void )
  132. {
  133.    return (1);
  134. }
  135.  
  136.  
  137. /*----------------------------------------------------------------------------*/
  138. /* Procedure:  iOpenSrv                                                       */
  139. /*             A Station requiers a connect to a service                      */
  140. /*                                                                            */
  141. /* Parameters: ptOpenSrv                                                      */
  142. /*                                                                            */
  143. /* Returns:    1 if OK                                                        */
  144. /*             0 if conenction is refused, apply a text in ptOpenSrv->stError */
  145. /*               to explain the reason for refuse !                           */
  146. /*                                                                            */
  147. /*----------------------------------------------------------------------------*/
  148.  
  149.  
  150. int APIENTRY iOpenSrv ( OPEN_SERVICE *ptOpenSrv )
  151. {
  152.    CON_DATA *ptCon;
  153.  
  154.    ptCon = malloc(sizeof(CON_DATA));
  155.    memset ( ptCon, 0x00, sizeof(CON_DATA));
  156.  
  157.    ptCon->hCon=ptOpenSrv->hCon;
  158.    strcpy(ptCon->szCallSign,ptOpenSrv->szCallSign);
  159.    vAddCon(ptCon);
  160.  
  161.    // A maximum of 127 characters is permited in welcome text !
  162.    
  163.    strcpy(ptOpenSrv->szError,"Welcome to the demo service, ");
  164.    strcat(ptOpenSrv->szError,ptOpenSrv->szCallSign);
  165.    strcat(ptOpenSrv->szError," !\n\r");
  166.    strcat(ptOpenSrv->szError,"Enter //EXIT to exit service !");
  167.  
  168.    return (1);
  169. }
  170.  
  171.  
  172. /*----------------------------------------------------------------------------*/
  173. /* Procedure:  iCloseSrv                                                      */
  174. /*             A Connect is closed without colosing the Service before.       */
  175. /*                                                                            */
  176. /* Parameters: hCon  Connection Handle = Connect Window                       */
  177. /*                                                                            */
  178. /* Returns:    1 if OK                                                        */
  179. /*                                                                            */
  180. /*----------------------------------------------------------------------------*/
  181.  
  182.  
  183. int APIENTRY iCloseSrv( long hCon )
  184. {
  185.    vDelCon ( hCon );
  186.    return (1);
  187. }
  188.  
  189.  
  190. /*----------------------------------------------------------------------------*/
  191. /* Procedure:  iSnd2Srv                                                       */
  192. /*             The connected station sends a data package to ther service.    */
  193. /*                                                                            */
  194. /* Parameters: ptSrv contains connection handle und Daten package.            */
  195. /*                                                                            */
  196. /* Returns:    1 wenn OK                                                      */
  197. /*                                                                            */
  198. /*----------------------------------------------------------------------------*/
  199.  
  200.  
  201. int APIENTRY iSnd2Srv( RXTX_SERVICE *ptSrv )
  202. {
  203.    CON_DATA   *ptCon;
  204.    short      i;
  205.  
  206.    ptCon=ptGetCon(ptSrv->hCon);
  207.    
  208.    for ( i=0; i < ptSrv->sPacLen; i++)
  209.    {
  210.       if (ptSrv->sPacket[i] == CR || ptSrv->sPacLen >= 127 )
  211.       {
  212.          ptCon->szInpBuffer[ptCon->sCount]='\0';
  213.          vService ( ptSrv->hCon, ptCon );       // proccess a mplete Line
  214.          ptCon->sCount=0;
  215.          
  216.       }
  217.       else
  218.          if (ptSrv->sPacket[i] != LF)
  219.              ptCon->szInpBuffer[ptCon->sCount++]=ptSrv->sPacket[i];
  220.    }
  221.    return (1);
  222. }
  223.  
  224.  
  225. /*----------------------------------------------------------------------------*/
  226. /* Procedure:  iVersion                                                       */
  227. /*             Get the version ot the DLL.                                    */
  228. /*                                                                            */
  229. /* Parameters: void                                                           */
  230. /*                                                                            */
  231. /* Returns:    Version                                                        */
  232. /*                                                                            */
  233. /*----------------------------------------------------------------------------*/
  234.  
  235.  
  236. int APIENTRY iVersion ( void )
  237. {
  238.    return (VERSION);
  239. }
  240.  
  241.  
  242. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  243. /*                                                                         */
  244. /* vAddCon                                                                 */
  245. /*                                                                         */
  246. /* Appends a CON record to the linked structure.                           */
  247. /*                                                                         */
  248. /*                                                                         */
  249. /*                                                                         */
  250. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  251.  
  252. static void vAddCon ( CON_DATA  *ptCon )
  253. {
  254.    CON_DATA  *ptHlp;
  255.  
  256.    if ( ptFirstCon == NULL )
  257.       ptFirstCon=ptCon;
  258.    else
  259.    {
  260.       for ( ptHlp=ptFirstCon;
  261.             ptHlp->ptNextCon != NULL;
  262.             ptHlp = (CON_DATA *) ptHlp->ptNextCon
  263.           );
  264.  
  265.       ptHlp->ptNextCon = (void *) ptCon;
  266.    }
  267. }
  268.  
  269.  
  270. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  271. /*                                                                         */
  272. /* vDelCon                                                                 */
  273. /*                                                                         */
  274. /* Delets a CON record from the linked structure.                          */
  275. /*                                                                         */
  276. /*                                                                         */
  277. /*                                                                         */
  278. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  279.  
  280. static void vDelCon ( long hCon )
  281. {
  282.    CON_DATA   *ptHlp=NULL;
  283.    CON_DATA   *ptPrev=NULL;
  284.  
  285.    for ( ptHlp=ptFirstCon;
  286.          ptHlp!=NULL;
  287.          ptPrev=ptHlp, ptHlp=(CON_DATA *)ptHlp->ptNextCon )
  288.    {
  289.       if (ptHlp->hCon == hCon)
  290.       {
  291.         if (ptPrev)
  292.            ptPrev->ptNextCon=(void *)ptHlp->ptNextCon;
  293.         else
  294.            ptFirstCon=(void *)ptHlp->ptNextCon;
  295.  
  296.         free( (char *)ptHlp);
  297.         break;
  298.       }
  299.    }
  300. }
  301.  
  302.  
  303. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  304. /*                                                                         */
  305. /* vGetCon                                                                 */
  306. /*                                                                         */
  307. /* Gets the connection structure from a connection handle.                 */
  308. /*                                                                         */
  309. /*                                                                         */
  310. /*                                                                         */
  311. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  312.  
  313. static CON_DATA *ptGetCon ( long hCon )
  314. {
  315.    CON_DATA   *ptHlp=NULL;
  316.  
  317.    for ( ptHlp=ptFirstCon;
  318.          ptHlp!=NULL && ptHlp->hCon != hCon;
  319.          ptHlp=(CON_DATA *)ptHlp->ptNextCon );
  320.  
  321.    return ( ptHlp );
  322. }
  323.  
  324.  
  325.  
  326. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  327. /*                                                                         */
  328. /* vExitService                                                            */
  329. /*                                                                         */
  330. /* Ein Service wird con einem Connect beendet.  .                          */
  331. /*                                                                         */
  332. /*                                                                         */
  333. /*                                                                         */
  334. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  335.  
  336. static void vExitService ( long hCon, CON_DATA *ptCon )
  337. {
  338.    RXTX_SERVICE *ptData;
  339.  
  340.    ptData = malloc(sizeof(RXTX_SERVICE));
  341.    memset ( ptData, 0x00, sizeof(RXTX_SERVICE));
  342.  
  343.    ptData->hCon = hCon;
  344.    strcpy(ptData->sPacket,"Goodby from ECHO Service, ");
  345.    strcat(ptData->sPacket,ptCon->szCallSign);
  346.    strcat(ptData->sPacket," \n\r");
  347.    ptData->sPacLen=strlen(ptData->sPacket);
  348.    (*lpfnSnd2Con) (ptData);
  349.    (*lpfnControl) (ptCon->hCon, SRV_EXIT, NULL );
  350.    free ( (char *)ptData);
  351. }
  352.                          
  353.                         
  354.                          
  355. static void vService ( long hCon, CON_DATA *ptCon )
  356. {
  357.    RXTX_SERVICE *ptData;
  358.  
  359.  
  360.    if ( strstr(ptCon->szInpBuffer,"//EXIT") )
  361.    {
  362.       vExitService ( hCon, ptCon );
  363.       return;
  364.    }
  365.  
  366.    ptData = malloc(sizeof(RXTX_SERVICE));
  367.    memset ( ptData, 0x00, sizeof(RXTX_SERVICE));
  368.  
  369.    ptData->hCon = hCon;
  370.    strcpy(ptData->sPacket,"<");
  371.    strcat(ptData->sPacket,ptCon->szCallSign);
  372.    strcat(ptData->sPacket,">");
  373.    strcat(ptData->sPacket,ptCon->szInpBuffer);
  374.    strcat(ptData->sPacket,"\n\r");
  375.    
  376.    ptData->sPacLen=strlen(ptData->sPacket);
  377.    (*lpfnSnd2Con) (ptData);
  378.    free ( (char *)ptData);
  379. }
  380.